﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;

namespace SmartWMS.Common.Signalr
{
    public class ChatHub: Hub
    {
        /// <summary>
        /// 接收前端消息，并发送给连接的全部用户
        /// </summary>
        /// <param name="user"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public async Task SendMessage(string user, string message = "")
        {
            //可以加缓存，存储用户信息和对应的连接Id
            await Clients.All.SendAsync("GetMessage", user, message);
        }

        ///// <summary>
        ///// 重写基类方法，连接成功后，调用前端方法，可以写业务逻辑。这里是发送一个连接标识
        ///// </summary>
        ///// <returns></returns>
        //public override async Task OnConnectedAsync()
        //{
        //    await Clients.All.SendAsync("ConnectMessage", Context.ConnectionId);
        //}

        ///// <summary>
        ///// 重写基类方法，退出连接
        ///// </summary>
        ///// <returns></returns>
        //public override async Task OnDisconnectedAsync(Exception? exception)
        //{
        //    //可以从缓存，返回对应掉线用户，同时清除改用户缓存
        //    await Clients.All.SendAsync("ConnectMessage", Context.ConnectionId, exception?.Message);
        //}
    }
}
